//
//  CommentKey.m
//  Molian
//
//  Created by 胡津 on 15/3/27.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "CommentKey.h"

@implementation CommentKey
@synthesize mDelegate;

static CommentKey *customKeyboard = nil;

//+(CommentKey *)customKeyboard
//{
//    @synchronized(self)
//    {
//        if (customKeyboard == nil)
//        {
//            customKeyboard = [[CommentKey alloc] init];
//        }
//        return customKeyboard;
//    }
//}
//+(id)allocWithZone:(struct _NSZone *)zone //确保使用者alloc时 返回的对象也是实例本身
//{
//    @synchronized(self)
//    {
//        if (customKeyboard == nil)
//        {
//            customKeyboard = [super allocWithZone:zone];
//        }
//        return customKeyboard;
//    }
//}
//+(id)copyWithZone:(struct _NSZone *)zone //确保使用者copy时 返回的对象也是实例本身
//{
//    return customKeyboard;
//}

-(void)textViewShowView:(UIViewController *)viewController customKeyboardDelegate:(id)delegate
{
    self.mViewController =viewController;
    self.mDelegate =delegate;
    self.isTop = NO;//初始化的时候设为NO
    
    self.mBackView =[[UIToolbar alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-50, DeviceWidth, 50)];
    [self.mViewController.view addSubview:self.mBackView];
    
    self.mSecondaryBackView =[[UIView alloc]initWithFrame:CGRectMake(10, 10, DeviceWidth-100, 30)];
    [self.mBackView addSubview:self.mSecondaryBackView];
    
    self.mTextView =[[UITextView alloc]initWithFrame:CGRectMake(1, 1, DeviceWidth-98, 28)];
    self.mTextView.backgroundColor =[UIColor whiteColor];
    self.mTextView.delegate = self;
    self.mTextView.layer.cornerRadius = 28/2;
    self.mTextView.layer.masksToBounds = YES;
    self.mTextView.delegate = self;
    self.mTextView.layer.borderWidth = 1;
    self.mTextView.layer.borderColor = [RGBA(205, 205, 205, 1) CGColor];
    self.mTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;;
    [self.mSecondaryBackView addSubview:self.mTextView];
    
    self.placeholder =[[UILabel alloc]initWithFrame:CGRectMake(7, 1, DeviceWidth-98, 28)];
    self.placeholder.text = @"点击评论...";
    self.placeholder.font = [UIFont fontWithName:FontName size:13];
    self.placeholder.textColor = [UIColor lightGrayColor];
    [self.mSecondaryBackView addSubview:self.placeholder];
    
    
    
    self.mTalkBtn = [[BFPaperButton alloc]init];
    self.mTalkBtn.frame = CGRectMake(DeviceWidth-80, 11, 70, 28);
    [self.mTalkBtn setBackgroundColor:APPCOLOR(1)];
    [self.mTalkBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.mTalkBtn.tapCircleColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.mTalkBtn.layer.masksToBounds = YES;//开启圆角
    [self.mTalkBtn.layer setCornerRadius:CGRectGetHeight([self.mTalkBtn bounds]) / 2];
    self.mTalkBtn.titleLabel.font = [UIFont fontWithName:FontName size:14];
    [self.mTalkBtn setTitle:@"评论" forState:UIControlStateNormal];
    [self.mTalkBtn addTarget:self action:@selector(forTalk) forControlEvents:UIControlEventTouchUpInside];
    [self.mTalkBtn setTintColor:[UIColor blackColor]];
    [self.mBackView addSubview:self.mTalkBtn];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
    
}

-(void)textRecover
{
//    self.mBackView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-50, DeviceWidth, 50);
//    self.mSecondaryBackView.frame = CGRectMake(10, 10, DeviceWidth-100, 30);
//    self.mTextView.frame = CGRectMake(1, 1, DeviceWidth-98, 28);
//    self.mTalkBtn.frame = CGRectMake(DeviceWidth-80, 11, 70, 28);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.mTextView resignFirstResponder];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [self placeholderAlpha];

    
    return YES;
}

-(void)forTalk //评论按钮
{
    [self placeholderAlpha];

    if (self.mTextView.text.length==0)
    {
        [self.mTextView becomeFirstResponder];
        NSLog(@"内容为空");
        [HUD Prompt:@"请输入内容"];
    }
    else
    {
        [self.mTextView resignFirstResponder];
        if ([mDelegate respondsToSelector:@selector(talkBtnClick:) ]) {
            [mDelegate talkBtnClick:self.mTextView];
        }
        
    }
    
}

-(void)hideView //点击屏幕其他地方 键盘消失
{
    NSLog(@"屏幕消失");
    [self placeholderAlpha];
    [self.mTextView resignFirstResponder];
}

- (void)keyboardWillShow:(NSNotification*)notification //键盘出现
{
    self.isTop =YES;
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [self placeholderAlpha];

    if (!self.mHiddeView)
    {
        self.mHiddeView =[[UIView alloc]initWithFrame:CGRectMake(0, 0,DeviceWidth,DeviceHeight)];
        self.mHiddeView.backgroundColor =[UIColor blackColor];
        self.mHiddeView.alpha =0.0f;
        [self.mViewController.view addSubview:self.mHiddeView];
        
        UIButton *hideBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        hideBtn.backgroundColor =[UIColor clearColor];
        hideBtn.frame = self.mHiddeView.frame;
        [hideBtn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
        [self.mHiddeView addSubview:hideBtn];
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.mHiddeView.alpha =0.4f;
        self.mBackView.frame =CGRectMake(0, DeviceHeight-_keyboardRect.size.height-self.mBackView.bounds.size.height, DeviceWidth, self.mBackView.bounds.size.height);
        [self.mViewController.view bringSubviewToFront:self.mBackView];
        self.keyhHigh = self.mBackView.bounds.size.height;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)keyboardWillHide:(NSNotification*)notification //键盘下落
{
    [self placeholderAlpha];
    self.isTop =NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.mBackView.frame=CGRectMake(0, DeviceHeight- self.mBackView.bounds.size.height, DeviceWidth, self.mBackView.bounds.size.height);
        self.mHiddeView.alpha =0.0f;
        self.keyhHigh = self.mBackView.bounds.size.height;
//        self.mSecondaryBackView.frame =CGRectMake(10, 10, DeviceWidth-100,30);
//        self.mTalkBtn.frame = CGRectMake(DeviceWidth-90, 1, 70, 30);
    } completion:^(BOOL finished) {
        [self.mHiddeView removeFromSuperview];
        self.mHiddeView =nil;
        //        self.mTextView.text =@"我来说两句..."; //键盘消失时，恢复TextView内容
    }];
}
- (void)textDidChanged:(NSNotification *)notif //监听文字改变 换行时要更改输入框的位置
{
    [self placeholderAlpha];
    CGSize contentSize = self.mTextView.contentSize;
    
    if (contentSize.height > 140){
        return;
    }
    CGFloat minus = 3;
    CGRect selfFrame = self.mBackView.frame;
    CGFloat selfHeight = self.mTextView.superview.frame.origin.y * 2 + contentSize.height - minus + 2 * 2;
    CGFloat selfOriginY = selfFrame.origin.y - (selfHeight - selfFrame.size.height);
    selfFrame.origin.y = selfOriginY;
    selfFrame.size.height = selfHeight;
    self.mBackView.frame = selfFrame;
    self.mSecondaryBackView.frame =CGRectMake(10, 10, DeviceWidth-100, selfHeight-20);
    self.mTalkBtn.frame = CGRectMake(DeviceWidth-80, 11, 70, selfHeight-20);
    self.keyhHigh = self.mBackView.bounds.size.height;
}

-(void)placeholderAlpha{
    if (self.mTextView.text.length >= 1) {
        self.placeholder.alpha = 0;
    }else{
        self.placeholder.alpha = 1;
    }
}
-(void)dealloc //移除通知
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

@end

